package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.hotent.base.core.util.string.StringUtil;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.service.IDataBean;
import com.maoye.ossp.common.util.ApplicationContextUtils;
import com.maoye.ossp.common.util.RequestContext;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.ContractArticle;
import com.maoye.ossp.sys.base.entity.ContractRateCode;
import com.maoye.ossp.sys.base.service.ContractArticleService;
/**
 * 合同条款信息
 * @author linzhizhong
 *
 */
@Controller
@RequestMapping(value = "sys/base/article/")
public class ContractArticleController extends BaseController {
   
	  @Resource
	  ContractArticleService service;
	

	@RequestMapping(value = "list")
	public String List(ContractArticle article,HttpServletRequest request,HttpServletResponse response, Model model){
		QueryFilter queryFilter = getQuerFilter(request,article);
		if(StringUtil.isNotEmpty(article.getArticle())){
			queryFilter.addFilter("article", article.getArticle(), QueryOP.LIKE);
		}
		Page<ContractArticle> page = service.find(queryFilter);
		
		model.addAttribute("page", page);
		return resultPage("/sys/base/article//list", model, request);

	}
	
	
	
	@RequestMapping(value = "add")
	public String add(ContractArticle contractArticle, Model model,HttpServletRequest request) throws Exception 
	{

		return resultPage("/sys/base/article//add", model, request) ;
	}
	
	@RequestMapping(value = "save")
	public String save(ContractArticle contractArticle, Model model,HttpServletRequest request) throws Exception {
		contractArticle.setId(idGenerator.getUId());
		service.create(contractArticle);
		return "redirect:/sys/base/article//list" ;
	}
	
	
	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id,Model model,HttpServletRequest request) throws Exception {
		ContractArticle contractArticle =service.get(Long.parseLong(id));
		model.addAttribute("contractArticle", contractArticle);
		
		return resultPage("/sys/base/article/view",model,request);
	}
	
	
	
	/***
	 * 获取临时付款项目
	 * @param request
	 * @param response
	 * @param model
	 * @author eric
	 * @throws Exception 
	 * @data 2014年4月19日
	 */
	@ResponseBody
	@RequestMapping(value = "getArticle")
	
	public List<Object> getArticle(HttpServletRequest request,HttpServletResponse response, Model model) throws Exception{
		response.setContentType("application/json; charset=UTF-8");
		String s_bean = "selectArtictService";
		IDataBean dataBean = (IDataBean) ApplicationContextUtils.getBean(s_bean);
		RequestContext requestContext = new RequestContext(request);
		List<Object> dataList = dataBean.getDataList(requestContext);
		return dataList;
		
	}
	
	@RequestMapping(value = "getSysBaseRateCode")
	public void getSysBaseRateCode(HttpServletRequest request,HttpServletResponse response, Model model,String itemNum,String shopId) throws IOException{
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		HashMap<String, String>  map  =Maps.newHashMap();
		map.put("article", itemNum);
		map.put("shopId", shopId);
		ContractArticle contractArticle =service.getContractRateCodeByNumber(map);
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		int count=0;
		if(contractArticle==null){
			return;
		}
		for (ContractRateCode racteCode : contractArticle.getRateCode()) {
			count++;
			sb.append("{");
			sb.append("\"article\":");
			sb.append("\""+racteCode.getRateCode()+"\"");
			sb.append(",");
			sb.append("\"articleName\":");
			if(racteCode.getSysRateCode()!=null){
			sb.append("\""+racteCode.getSysRateCode().getTaxRateRemark()+"\"");
			}
			sb.append(",");
			sb.append("\"salestype\":");
			sb.append("\""+racteCode.getSalestype()+"\"");
			sb.append("}");
			if(contractArticle.getRateCode().size()!=count){
			 sb.append(",");
			}
		}
		sb.append("]");
		out.write(sb.toString());
		out.flush();
		out.close();
	}
}


